package memberrewards.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

import com.google.appengine.api.datastore.Text;

@Entity
public class CatalogItem {
	@Id
	private String itemid;
	private String name;
	private String make;
	private Text description;
	private int pointCost;
	private String eligibleTiers;
	@Transient
	private Collection<String> tierList;

	public CatalogItem(String itemId, String name, String description,
			String make, int cost,String eligTiers) {
		this.itemid = itemId;
		this.name = name;
		this.make = make;
		this.description = new Text(description);
		this.pointCost = cost;
		setEligibleTiers(eligTiers);
	}

	public String getItemid() {
		return itemid;
	}

	public void setItemid(String itemId) {
		this.itemid = itemId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMake() {
		return make;
	}

	public void setMake(String make) {
		this.make = make;
	}

	public String getDescription() {
		return description.getValue();
	}

	public void setDescription(Text description) {
		this.description = description;
	}

	public int getPointCost() {
		return pointCost;
	}

	public void setPointCost(int cost) {
		this.pointCost = cost;
	}
	public String getEligibleTiers() {
		return eligibleTiers;
	}

	public void setEligibleTiers(String eligTiers) {
		this.eligibleTiers = eligTiers;
		populateTierList();
	}
	
	private void populateTierList(){
		if(getEligibleTiers()!=null){
			tierList= new ArrayList<String>();
			StringTokenizer tokenizer= new StringTokenizer(getEligibleTiers(),",");
			String tier=null;
			while(tokenizer.hasMoreTokens()){
				tier=tokenizer.nextToken();
				tierList.add(tier);
			}
		}
	}

	public Collection<String> getTierList() {
		if(this.tierList==null){
			populateTierList();
		}
		return tierList;
	}
}
